unit Consts;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This file is part of the PasCalc Calculator

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, windows;

type

  { TfrmConsts }

  TfrmConsts = class(TForm)
    btnEdit: TButton;
    btnConstToEd: TButton;
    ConstList: TListBox;
    procedure btnConstToEdClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    procedure LangInit(strs: TStringList);
  end; 

var
  frmConsts: TfrmConsts;
  edExpr: TEdit;

implementation

{ TfrmConsts }

procedure _SetFocus(Edit: TEdit; Pos: integer);
begin
  Edit.SelStart := Pos;
  Windows.SetFocus(Edit.Handle);
end;

procedure StrIns(Edit: TEdit; const str: AnsiString;
  const IsBr: boolean = false);
var
  CurPos: integer;
begin
  CurPos := Edit.SelStart;
  Edit.SelText := Str;
  if IsBr then _SetFocus(Edit, CurPos + Length(Str) - 1) else
     _SetFocus(Edit, CurPos + Length(Str));
end;

procedure TfrmConsts.btnEditClick(Sender: TObject);
begin
  ShellExecute(Handle, 'open', 'c:\windows\notepad.exe', 'Consts.txt', nil, SW_SHOWNORMAL);
end;

procedure TfrmConsts.FormShow(Sender: TObject);
begin
  if not FileExists('consts.txt') then
  begin
    ConstList.Items.Text := 'Файл с константами не обнаружен';
    exit;
  end;
  ConstList.Items.LoadFromFile('consts.txt');
  ConstList.ItemIndex := ConstList.Items.Count - 1;
end;

procedure TfrmConsts.LangInit(strs: TStringList);
begin
  frmConsts.Caption := strs[52];
  btnEdit.Caption := strs[53];
  btnConstToEd.Caption := strs[54];
end;

procedure TfrmConsts.btnConstToEdClick(Sender: TObject);
var
  ConstStart: integer;
  Str: ANSIString;
begin
  Str := ConstList.Items[ConstList.ItemIndex];
  ConstStart := ANSIPos('=', Str);
  Str := Copy(str, ConstStart+2, Length(Str) - ConstStart - 1);
  StrIns(edExpr, Str);
end;

initialization
  {$I Consts.lrs}

end.

